package com.suray.system.run;

import com.suray.basic.wcs.utils.LoggerUtil;
import com.suray.basic.wcs.utils.ParamConfig;
import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.wcs.service.system.entity.WareHouseDB;
import com.suray.wcs.service.system.service.IWareHouseDBService;
import com.suray.wcs.service.util.WcsUtil;
import org.springframework.context.annotation.DependsOn;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@DependsOn(value = {"springContextHolder"})
public class SurayApplication {

    private AppTask appTask;

    private long sysTimeTick = 0;               //10ms = 1tick
    private final long cycle = 10;              //ms, %10 = 0

    public void SurayAppInit() {
        try {
            appTask = new AppTask();

            init();
            run();
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
        }
    }

    private void initParam() {
        IWareHouseDBService wareHouseDBService = SpringContextHolder.getBean("wcsDistrictService");
        WareHouseDB curWareHouseDB = wareHouseDBService.getCurWareHouseDB();
        ParamConfig.isDebugModel = "1".equals(curWareHouseDB.getDebugModel());
    }

    private void init() {
        initParam();
        appTask.initSystem();
        appTask.initCommunication();
        appTask.initDevice();
        appTask.initFunction();
    }

    private void moduleRun() {
        appTask.task10ms();

        if (sysTimeTick % 2 == 0) {
            appTask.task20ms();
        }

        if (sysTimeTick % 5 == 0) {
            appTask.task50ms();
        }

        if (sysTimeTick % 10 == 0) {
            appTask.task100ms();
        }

        if (sysTimeTick % 20 == 0) {
            appTask.task200ms();
        }

        if (sysTimeTick % 50 == 0) {
            appTask.task500ms();
        }

        if (sysTimeTick % 100 == 0) {
            appTask.task1s();
        }

        if (sysTimeTick % 200 == 0) {
            appTask.task2s();
        }

        if (sysTimeTick % 500 == 0) {
            appTask.task5s();
        }

        if (sysTimeTick % 6000 == 0) {
            appTask.task1min();
        }

        if (sysTimeTick % 12000 == 0) {
            appTask.task2min();
        }

        if (sysTimeTick % 30000 == 0) {
            appTask.task5min();
        }

        if (sysTimeTick % 360000 == 0) {
            appTask.task1hour();
            sysTimeTick = 0;
        }
    }

    private void run() {
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.scheduleWithFixedDelay(() -> {
            try {
                sysTimeTick++;
                moduleRun();
            } catch (Exception e) {
                WcsUtil.exceptionLog(e, this.getClass());
            }
        }, 0, cycle, TimeUnit.MILLISECONDS);

        LoggerUtil.info("模块运行线程启动！", this.getClass());
    }
}
